﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        string cmdString = "select * from Articles"; 

        private void MainForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DbHelper.GetTable(cmdString);
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string seachName = textSeach.Text;
            string seach = string.Format("select * from Articles where Title like '%{0}%' or Author like '%{0}%'", seachName);
            dataGridView1.DataSource = DbHelper.GetTable(seach);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var res = editForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
                string seachName = textSeach.Text;
                string seach = string.Format("select * from Articles where Title like '%{0}%'", seachName);
                dataGridView1.DataSource = DbHelper.GetTable(seach);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;

                EditForm editForm = new EditForm(id, title,author,createdTime);

                var res = editForm.ShowDialog();
                if(res.Equals(DialogResult.Yes))
                {
                    string seachName = textSeach.Text;
                    string seach = string.Format("select * from Articles where Title like '%{0}%'", seachName);
                    dataGridView1.DataSource = DbHelper.GetTable(seach);
                }

            }
            else
            {
                MessageBox.Show("未选择任何行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定删除吗","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var delete = string.Format("delete from Articles where Id = '{0}'",id);
                    var rowcount = DbHelper.AddOrEditOrDeleSave(delete);

                    if (rowcount > 0)
                    {
                        dataGridView1.DataSource = DbHelper.GetTable(cmdString);
                        MessageBox.Show("删除成功","提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("未选择删除行","提示");
            }
        }
    }
}
